FaizanAnwerAli / forum.mobilism.org Code Highlighting JavaScript [Beta] by (GL)BigBrother

// ==UserScript==
// @id              codeHiglighting
// @name            forum.mobilism.org Code Highlighting JavaScript [Beta] by (GL)BigBrother
// @version         0.2.3
// @description     forum.mobilism.org or mobilism.me syntax highlighting in code tag. Thanks to highlightjs.org for highlight.js and css themes.
// @icon            http://cdn4.aptoide.com/imgs/2/e/f/2efe668367ff1036f99de8f76b3b8381.png
// @icon64          http://i.imgur.com/mWrE59h.png
// @author          Faizan Anwer Ali <faizananwer2011@gmail.com>
// @match           http://forum.mobilism.org/*
// @match           https://forum.mobilism.org/*
// @match           http://mobilism.me/+
// @match           https://mobilism.me/+
// @include         http://forum.mobilism.org/*
// @include         https://forum.mobilism.org/*
// @include         http://mobilism.me/+
// @include         https://mobilism.me/+
// @require         https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js
// @require         https://sites.google.com/site/faizanrupani2013/script/highlight.pack.js
// @grant           GM_getValue
// @grant           GM_setValue
// @grant           GM_xmlhttpRequest
// @grant           GM_openInTab
// ==/UserScript==

/* Information
********************************************************************************************
(GL)BigBrother's Homepage:      https://openuserjs.org/scripts/FaizanAnwerAli/forum.mobilism.org_Code_Highlighting_JavaScript_%5BBeta%5D_by_(GL)BigBrother
                                http://forum.mobilism.org/viewtopic.php?t=1090454
                                
------------------------------------
    Version History
------------------------------------

0.2.3 (3-June-2015)
 - Minor bug fixed.
 - updated higlight.js script to v8.6. Now it will auto update.

0.2.2 (10-May-2015)
 - Fixed small bug of not displaying <br> line of html in code tag.

0.2.1 (9-May-2015)
 - Remove highlighting from Quotes tag Button. (firefox bug fixed, now it'll work in all browser except IE.)

0.2 (8-May-2015)
 - Added 9 new themes. Total 63 themes.
      ("atelier-cave.dark","atelier-cave.light","atelier-estuary.dark","atelier-estuary.light","atelier-plateau.dark","atelier-plateau.light","atelier-savanna.dark","atelier-savanna.light","github-gist")
 - Auto css theme update.
 - Auto Script Update checking System.
 - Remove highlighting from Quotes tag Button. (Properly work only in Chrome, I'll fix firefox bug as soon as I know how to do that)

0.1 (17-Apr-2015)
 - Initial Release

*************************************************************************************************/


/*@require         http://yourjavascript.com/4405510561/highlight-pack.js*/

(function(){
    var data = '<span style="font-size: 12px;margin-left:5px;color: rebeccapurple;"><b>Select Theme for Code Highlighting</b></span><select id="Themes" onchange="jsfunction(this.value)" style="margin-left: 10px;"> <option id="default">default</option> <option id="disable">disable</option> <option id="agate">agate</option> <option id="androidstudio">androidstudio</option> <option id="arta">arta</option> <option id="ascetic">ascetic</option> <option id="atelier-cave.dark">atelier-cave.dark</option> <option id="atelier-cave.light">atelier-cave.light</option> <option id="atelier-dune.dark">atelier-dune.dark</option> <option id="atelier-dune.light">atelier-dune.light</option> <option id="atelier-estuary.dark">atelier-estuary.dark</option> <option id="atelier-estuary.light">atelier-estuary.light</option> <option id="atelier-forest.dark">atelier-forest.dark</option> <option id="atelier-forest.light">atelier-forest.light</option> <option id="atelier-heath.dark">atelier-heath.dark</option> <option id="atelier-heath.light">atelier-heath.light</option> <option id="atelier-lakeside.dark">atelier-lakeside.dark</option> <option id="atelier-lakeside.light">atelier-lakeside.light</option> <option id="atelier-plateau.dark">atelier-plateau.dark</option><option id="atelier-plateau.light">atelier-plateau.light</option><option id="atelier-savanna.dark">atelier-savanna.dark</option><option id="atelier-savanna.light">atelier-savanna.light</option> <option id="atelier-seaside.dark">atelier-seaside.dark</option> <option id="atelier-seaside.light">atelier-seaside.light</option> <option id="atelier-sulphurpool.dark">atelier-sulphurpool.dark</option> <option id="atelier-sulphurpool.light">atelier-sulphurpool.light</option> <option id="brown_paper">brown_paper</option> <option id="codepen-embed">codepen-embed</option> <option id="color-brewer">color-brewer</option> <option id="dark">dark</option> <option id="darkula">darkula</option> <option id="docco">docco</option> <option id="far">far</option> <option id="foundation">foundation</option> <option id="github-gist">github-gist</option> <option id="github">github</option> <option id="googlecode">googlecode</option> <option id="hybrid">hybrid</option> <option id="idea">idea</option> <option id="ir_black">ir_black</option> <option id="kimbie.dark">kimbie.dark</option> <option id="kimbie.light">kimbie.light</option> <option id="magula">magula</option> <option id="mono-blue">mono-blue</option> <option id="monokai">monokai</option> <option id="monokai_sublime">monokai_sublime</option> <option id="obsidian">obsidian</option> <option id="paraiso.dark">paraiso.dark</option> <option id="paraiso.light">paraiso.light</option> <option id="pojoaque">pojoaque</option> <option id="railscasts">railscasts</option> <option id="rainbow">rainbow</option> <option id="school_book">school_book</option> <option id="solarized_dark">solarized_dark</option> <option id="solarized_light">solarized_light</option> <option id="sunburst">sunburst</option> <option id="tomorrow">tomorrow</option> <option id="tomorrow-night">tomorrow-night</option> <option id="tomorrow-night-blue">tomorrow-night-blue</option> <option id="tomorrow-night-bright">tomorrow-night-bright</option> <option id="tomorrow-night-eighties">tomorrow-night-eighties</option> <option id="vs">vs</option> <option id="xcode">xcode</option> <option id="zenburn">zenburn</option></select><button id="goButtonClick" style="margin-left: 8px; font-size: 12px;width: 30px;">Go</button>';
    var body = document.body || document.getElementsByTagName('body')[0];
    var div = document.createElement('div');
    div.id = 'storevalue';
    div.innerHTML = 'default';
    body.appendChild(div);
    $("#storevalue").hide();
    var slides = document.getElementsByClassName("codecontent");
    if(slides.length > 0) {
        $(".tablebg").eq(2).before(data);
    }
    var s = document.getElementsByClassName("uncited");
    for ( var i = 0; i < s.length; i++)
    {
        var mainClass = document.getElementsByClassName("uncited")[i];
        var childDiv = mainClass.getElementsByTagName('div')[0];
        childDiv.className = "codetag";
    }
    var tag1 = document.getElementsByTagName("code");
    var tag2 = document.getElementsByClassName("codetag");
    if(tag1.length > 0 || tag2.length > 0) {
        var slides2 = document.getElementsByClassName("pull-right");
        for ( var i = 0; i < slides2.length; i++)
        {
            var strr = document.getElementsByClassName("pull-right")[i].innerHTML;
            var nn = strr.lastIndexOf('<div class="pagination pagination-small hidden-phone">');
            var mm = nn + 54;
            var ress = strr.slice(nn, mm);
            if (ress === '<div class="pagination pagination-small hidden-phone">')
            {
                var pullrightinnerhtml = document.getElementsByClassName("pull-right")[i].innerHTML;
                document.getElementsByClassName("pull-right")[i].innerHTML = data + pullrightinnerhtml;
                break;
            }
        }
    }
    var head = document.head || document.getElementsByTagName('head')[0];
    var scripttag = document.createElement('script');
    scripttag.innerHTML = 'function jsfunction(value){document.getElementById("storevalue").innerHTML = value;}';
    head.appendChild(scripttag);
    var theme = GM_getValue("themes", "default");
    document.getElementById(theme).selected = "true";
    var cssurl = 'https://raw.githubusercontent.com/isagalaev/highlight.js/master/src/styles/'+theme+'.css';
    if (theme !== "disable") {
        GM_xmlhttpRequest({
            method: "GET",
            url: cssurl,
            onload: function(xhr) {
                var style = document.createElement('style');
                style.type = 'text/css';
                var css = '.codebox span {text-transform: none;border-bottom: none; margin-bottom: 3px; font-size: 0.999em; font-weight: bold; display: inline;} .codecontent {font-size: 12px;} .codetag{font-size: 12px!important;}' + xhr.responseText;
                if (style.styleSheet) {
                    style.styleSheet.cssText = css;
                } else {
                    style.appendChild(document.createTextNode(css));
                }
                head.appendChild(style);
            }
        });
    }
    $( "#goButtonClick" ).click(function() {
        var mytheme = document.getElementById("storevalue").innerHTML;
        GM_setValue("themes", mytheme);
        location.reload();
    });
    hljs.configure({useBR: true, tabReplace: '    ',});
    hljs.initHighlighting();
    $('.codecontent').each(function(i, block) {
        hljs.highlightBlock(block);
    });
    $('code').each(function(i, block) {
        hljs.highlightBlock(block);
    });
    var slides3 = document.getElementsByClassName("quotetitle");
    for ( var i = 0; i < slides3.length; i++)
    {
        var strr = document.getElementsByClassName("quotetitle")[i].innerHTML;
        document.getElementsByClassName("quotetitle")[i].innerHTML += '<button  class="showHighlightingButton" style="cursor: pointer; height: 20px; width: 140px; margin-left: 20px; ">Show Highlighting</button>';
    }
    var slides444 = document.getElementsByClassName("uncited");
    for ( var i = 0; i < slides444.length; i++)
    {
        var strr = document.getElementsByClassName("uncited")[i].innerHTML;
        document.getElementsByClassName("uncited")[i].innerHTML = '<button class="showHighlightingButton" style="cursor: pointer;   margin-left: 10px; position: relative;top: -5px;">Show Highlighting</button>' + strr;
    }
    var slidess = document.getElementsByTagName("blockquote");
    for ( var i = 0; i < slidess.length; i++)
    {
        var strr = slidess[i].innerHTML;
        slidess[i].innerHTML = '<button class="showHighlightingButton" style="cursor: pointer;   margin-left: 10px; position: relative;top: -5px;">Show Highlighting</button>' + strr;
    }
    var slidesss = document.getElementsByClassName("showHighlightingButton");
    var checkcheck ='';
    for ( var i = 0; i < slidesss.length; i++)
    {
        var idid = 'showHighlightingButton' + i;
        document.getElementsByClassName("showHighlightingButton")[i].id = idid;
        document.getElementById(idid).addEventListener("click", function(){some_function(this.id);});
    }
    function some_function(aa){
        var storeIdValue = aa;
        var res = storeIdValue.slice(22,storeIdValue.length);
        var a = parseInt(res);
        var classexsist = document.getElementsByClassName("quotecontent");
        if(classexsist.length > 0) {
            if ($('.quotecontent').eq(a).hasClass('hljs')) {
                document.getElementById(storeIdValue).innerHTML = 'Show Highlighting';
                document.getElementsByClassName("quotecontent")[a].innerHTML = document.getElementsByClassName("quotecontent")[a].innerHTML.replace(/\<br\>/gi, "brlinebrlinebrlinebrlinebrlinebrlinebrlinebrlinebrlinebrlinebrlinebrlinebrlinebrlinebrline");
                document.getElementsByClassName("quotecontent")[a].textContent = document.getElementsByClassName("quotecontent")[a].textContent.replace(/brlinebrlinebrlinebrlinebrlinebrlinebrlinebrlinebrlinebrlinebrlinebrlinebrlinebrlinebrline/gi, "bvuyhasdiuadbkuasdbkuwbiuva835ui3247iihijnasdh9asd81b2ibsad9o8lkabsf91bqo87dashjhbkasb<br>");
                document.getElementsByClassName("quotecontent")[a].innerHTML = document.getElementsByClassName("quotecontent")[a].innerHTML.replace(/bvuyhasdiuadbkuasdbkuwbiuva835ui3247iihijnasdh9asd81b2ibsad9o8lkabsf91bqo87dashjhbkasb\&lt\;br\&gt\;/gi, "<br>");
                var x = document.querySelectorAll(".quotecontent");
                var languageClass = x[a].className;
                var languageName = languageClass.slice(18, languageClass.length);
                $('.quotecontent').eq(a).removeClass('hljs');
                $('.quotecontent').eq(a).removeClass(languageName);
            }
            else {
                document.getElementById(storeIdValue).innerHTML = 'Remove Highlighting';
                $('.quotecontent').eq(a).each(function(i, block) {
                    hljs.highlightBlock(block);
                });
            }
        } else {
            if ($('.codetag').eq(a).hasClass('hljs')) {
                document.getElementById(storeIdValue).innerHTML = 'Show Highlighting';
                document.getElementsByClassName("codetag")[a].innerHTML = document.getElementsByClassName("codetag")[a].innerHTML.replace(/\<br\>/gi, "brlinebrlinebrlinebrlinebrlinebrlinebrlinebrlinebrlinebrlinebrlinebrlinebrlinebrlinebrline");
                document.getElementsByClassName("codetag")[a].textContent = document.getElementsByClassName("codetag")[a].textContent.replace(/brlinebrlinebrlinebrlinebrlinebrlinebrlinebrlinebrlinebrlinebrlinebrlinebrlinebrlinebrline/gi, "bvuyhasdiuadbkuasdbkuwbiuva835ui3247iihijnasdh9asd81b2ibsad9o8lkabsf91bqo87dashjhbkasb<br>");
                document.getElementsByClassName("codetag")[a].innerHTML = document.getElementsByClassName("codetag")[a].innerHTML.replace(/bvuyhasdiuadbkuasdbkuwbiuva835ui3247iihijnasdh9asd81b2ibsad9o8lkabsf91bqo87dashjhbkasb\&lt\;br\&gt\;/gi, "<br>");
                $('.codetag').eq(a).removeClass('hljs');
                var x = document.querySelectorAll(".codetag");
                var languageClass = x[a].className;
                var languageName = languageClass.slice(13, languageClass.length);
                $('.codetag').eq(a).removeClass(languageName);
            }
            else {
                document.getElementById(storeIdValue).innerHTML = 'Remove Highlighting';
                $('.codetag').eq(a).each(function(i, block) {
                    hljs.highlightBlock(block);
                });
            }
        }
    }
    var currentVersion = GM_info.script.version;
    getXML();
    function createXMLHttpRequest() {
        try { return new XMLHttpRequest(); } catch(e) {}
        try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
        try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
        alert("XMLHttpRequest not supported");
        return null;
    }
    function getXML() {
        var xhReq = createXMLHttpRequest();
        var server = 'https://sites.google.com/site/faizanrupani2013/xml/HighlightingVersion.xml';
        xhReq.open("GET", "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20xml%20where%20url%3D'" + server.replace(/ /,'%2520') + "'", false);
        xhReq.onreadystatechange=function(){
            if (xhReq.readyState == 4) {
                var rawXML = xhReq.responseXML;
                var serverVersion = rawXML.getElementsByTagName("version")[0].innerHTML.toString();
                if (serverVersion !== currentVersion) {
                    if (confirm('New update is available for Higlighting Script\nDo you want update it?')) {
                        GM_openInTab("https://openuserjs.org/scripts/FaizanAnwerAli/forum.mobilism.org_Code_Highlighting_JavaScript_%5BBeta%5D_by_(GL)BigBrother");
                    }
                }
            }
        }
        xhReq.send(null);
    }
})();